package impl.repository;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import dao.AccessDatabase;
import domain.Address;
import domain.CartItem;
import domain.Customer;
import domain.Product;
import domain.ProductApparel;
import interfaces.repository.CartItemRepository;
import interfaces.repository.ProductApparelRepository;

public class StubCartItemRepositoryImpl implements CartItemRepository {

	@Override
	public boolean create(Customer customer) {

		try {
			AccessDatabase a = new AccessDatabase();
			Connection con = a.conectar();
			Statement st = con.createStatement();
			st.execute("INSERT INTO `car` (`idCustomer`) VALUES ('"
					+ customer.getId() + "');");
			con.close();

			return true;
		} catch (Exception t) {

			return false;

		}

	}

	@Override
	public boolean addItem(int idCustomer,CartItem cartItem, Product product) {

		try {
			AccessDatabase a = new AccessDatabase();
			Connection con = a.conectar();
			Statement st = con.createStatement();
			st.execute("INSERT INTO `cartitem` (`idCustomer`,`idProduct`,`qtd`,`price`) VALUES ('"
					+ idCustomer
					+ "','"
					+ product.getId()
					+ "','"
					+ cartItem.getQuantity()
					+ "','"
					+ product.getPrice()
					+ "');");
			con.close();

			return true;
		} catch (Exception t) {

			return false;

		}

	}

	@Override
	public boolean deleteItem(int idCustomer, Product product) {

		try {
			AccessDatabase a = new AccessDatabase();
			Connection con = a.conectar();
			Statement st = con.createStatement();
			st.execute("DELETE FROM `cartitem` WHERE `idcartitem`='"
					+ product.getId() + "'AND '"+idCustomer+"'");
			con.close();

			return true;
		} catch (Exception t) {

			return false;

		}

	}

	@Override
	public List<CartItem> findAllbyIDCustomer(int idCustomer) {

		StubProductRepositoryImpl spri = new StubProductRepositoryImpl();
		ArrayList<CartItem> itens = new ArrayList<CartItem>();
		CartItem item;

		try {

			AccessDatabase a = new AccessDatabase();
			Connection con = a.conectar();
			Statement st = con.createStatement();
			ResultSet rs = st
					.executeQuery("SELECT * FROM cartitem WHERE idCustomer='"
							+ idCustomer + "'");

			while (rs.next()) {
				item = new CartItem(spri.findProductByID(rs.getInt("idProduct")),rs.getInt("qtd"), rs.getFloat("discount"));
				itens.add(item);
			}
			con.close();
			return itens;
		} catch (SQLException ex) {
			return null;
		}
	}

	@Override
	public boolean deleteAll() {

		try {
			AccessDatabase a = new AccessDatabase();
			Connection con = a.conectar();
			Statement st = con.createStatement();
			st.execute("DELETE FROM `cartitem`");
			con.close();

			return true;
		} catch (Exception t) {

			return false;

		}
	}
}
